/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.virtual.model;

/**
 * This class represents the simple XPATH queries generated by the VirtualInstance
 * 
 * @author Walter Luetgenau
 */
public class SimpleQuery {

    String path;
    String nodeType;
    String referenceProperty;
    int referenceDirection = REFERENCE_NONE;
    public static final int REFERENCE_TO_THIS = 1;
    public static final int REFERENCE_FROM_THIS = 2;
    public static final int REFERENCE_NONE = 3;
    
    public SimpleQuery() {}
    
    public SimpleQuery(String query) {
        setQueryParametersFromString(query);
    }

    public String getNodeType() {
        return nodeType;
    }

    public void setNodeType(String nodeType) {
        this.nodeType = nodeType;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public boolean isValidPath() {
        if (path.indexOf(".") < 0 && path.indexOf("primary") < 0) // not a path relative to a primary. Such pathes are not supported by jackrabbit
            return true;
        return false;
    }

    public int getReferenceDirection() {
        return referenceDirection;
    }

    public void setReferenceDirection(int referenceDirection) {
        this.referenceDirection = referenceDirection;
    }

    public String getReferenceProperty() {
        return referenceProperty;
    }

    public void setReferenceProperty(String referenceProperty) {
        this.referenceProperty = referenceProperty;
    }
    protected static final String ELEMENT = "/element(*,";
    protected static final String FROM_THIS = "[@jcr:uuid = 'primary(@";

    public String getQueryStringFromQueryParameters() {
        String query = path + ELEMENT + nodeType + ")";
        if (referenceProperty != null)
            switch (referenceDirection) {
                case REFERENCE_TO_THIS:
                    query = query + "[@" + referenceProperty + "= 'primary(@jcr:uuid)']";
                    break;

                case REFERENCE_FROM_THIS:
                    query = query + FROM_THIS + referenceProperty + ")']";
                    break;
            }

        return query;
    }

    public void setQueryParametersFromString(String query) {
        int indexElement = query.indexOf(ELEMENT);

        if (indexElement < 0) { // must be an new instance
            nodeType = query;
            return;
        }

        path = query.substring(0, indexElement);
        nodeType = query.substring(indexElement + ELEMENT.length(), query.indexOf(")", indexElement));

        int indexPrimary = query.indexOf("= 'primary(@jcr:uuid)'");
        if (indexPrimary >= 0) {
            referenceDirection = REFERENCE_TO_THIS;
            referenceProperty = query.substring(query.lastIndexOf("[@", indexPrimary)+2, indexPrimary);
        } else {
            int indexFromThis = query.indexOf(FROM_THIS);
            if (indexFromThis >= 0) {
                referenceDirection = REFERENCE_FROM_THIS;
                referenceProperty = query.substring(indexFromThis + FROM_THIS.length(), query.indexOf(")", indexFromThis));

            } else
                referenceDirection = REFERENCE_NONE;
        }

    }
}
